//file: common.h
#ifndef COMMON_H
#define COMMON_H

//	#define PROBE //comment out if building the lambda board
	#define FTRACE //echo function calls to UART
	#define FPGA //comment out for disabling fpga clocking, normal serial interface

	//#define debug(X) #ifdef FTRACE printf(X); #endif
	//#define UART_RFECHO //echo RF packets to UART
	

	#define flagset(X, Y) X |= Y;
	#define flagcheck(X, Y) X & Y
	///////////////////////////////////////
	//packet types between FPGA and Lambda board
	#define PACKET_BACKGND	0x01
	#define PACKET_SAO2		0x02
	#define PACKET_GRAPH	0x08
	#define PACKET_BPM	    0x04

	//RF packet types, highest nibble used as checksum
	#define RFPACKET_BEACON		0x08
	#define RFPACKET_BEACONACK 	0x09
	
	#define RFPACKET_SAO2 		0x02
	#define RFPACKET_BPM		0x03
	#define RFPACKET_STATUS		0x04
	#define RFPACKET_FTEST  	0x05 //call a specific function for test
	#define RFPACKET_ADCSTREAMH  0x06 //ADC stream high bits
	#define RFPACKET_ADCSTREAML  0x07//ADC stream low bits
	
	//bit flags of error types
	#define ERR_NONE 	0x00
	#define ERR_RFPROC 	0x01 //received rf data while rf packet is processed
	#define ERR_RFMODE  0x02
	#define ERR_RFPACKET 0x04 //RF packet lost

	//bit flags, generic
	#define FLAG_RFDATA_AVAIL 1 //data available from RF module (pin interrupt change)
	#define FLAG_RFDATA_RDY	2 //data ready to be transmitted
	#define FLAG_HEART_BEAT 4
	#define FLAG_SEND_GRAPH 8 //sent grpah packet through uart

	//////////////////////////////////////
	#define SLAVE_ADDR  0x10
	#define MESSAGEBUF_SIZE       4
	#define TWI_GEN_CALL         0x00  // The General Call address is 0

	// Sample TWI transmission commands
	#define TWI_CMD_MASTER_WRITE 0x10
	#define TWI_CMD_MASTER_READ  0x20

	// Sample TWI transmission states, used in the main application.
	#define SEND_DATA             0x01
	#define REQUEST_DATA          0x02
	#define READ_DATA_FROM_BUFFER 0x03
	//////////////////////////////////////
	#include "global.h"

	#include <avr/io.h> 
	#include <util/delay.h> 
	#include <stdio.h>
	#include <math.h>
	
	#include "serial.h"
	#include "USI_TWI_Master.h"
	///////////////////////
	#include "uart2.h"
	#include "RF24G.h"
	#include "cmdline.h"
	#include "a2d.h"
	#include "timer.h"
	///////////////////////
	#include "vt100.h"		// include vt100 terminal support
	#include "cmdline.h"	// include cmdline function library
	#include "rprintf.h"	// include printf function library
	//////////////////////////////////////
	extern volatile u08 Timer0Reg0;
	extern volatile u08 Timer1Reg0;
	extern u08 rf24g_mode;
	extern u08 rf_packet[4];

	u08 error; 
	u08 flags; //various flags
	//u08 graph_sample;
	#define BEEP_CHANNEL chan0
	u08 chan0;
	u08 chan1;
	u08 chan2;
	u08 chan3;
	u08 chan4;

	//functions declarations common to both boards
	void init_mcu(void);
	void init_rf(void);
	void init_ADC(void);
	void error_handler(void);
	void flag_handler(void);

	#warning "reminder: make ISR callbacks inline for optimization"
	void timer0Overflow(void); //immediately called by timer0 ISR
	void timer1Overflow(void); //immediately called by timer1 ISR

	void buzzerPOST(void);
	void LEDPOST(void);

	void packet_send_rf(void);
	void packet_recv_rf(void);

	void packet_rf_handler(void);
	//test functions
	void streamADC(void);
	
	//samy code
	#define rms_N 10
	#define dc_N 10
	
	#define N_AVG 3
	#define BPM_SIGNAL_CROSSOVER 50
	//#define ADC_SAMPLERATE 22118400/1024/256
	#define ADC_SAMPLERATE 22118400/256/256/2
	#warning "reminder: sample rate hard defined"

	u08 beep;
	u08 bpm;
	u08 noise;
	u08 avg_dc;
	u08 avgavg;
	
	u08 heartrate(unsigned char c);
	void packet_send_FPGA(u08 packet_type);
	#define FPGA_PACKET_LEN 5
	u08 fpga_packet[FPGA_PACKET_LEN];
	unsigned char rms(unsigned char c1);
	unsigned char avg(unsigned char c1);
//////////////////////////////////////
#ifdef PROBE
	void init_i2c();
	
	void lambdaprobe(void); //called in while loop of main()

	void LEDPOST2(void); //second status LED

	void dac_write(u08 addr, u08 voltage); //write value to DAC
	
	void adc_stream(void);//stream ADC data through RF
#else

	void lambdaboard(void);//called while loop of main()


#endif

#ifdef PROBE
	#define LAMBDA_STR "\nLambda Probe v0.7\n"
	#warning "***BUILDING PROBE"
	#warning "***BUILDING PROBE"
	#warning "***BUILDING PROBE"

//ports and pins
	//status LEDs
	#define LED_PIN 3
	#define LED_DDR DDRE
	#define LED_PORT PORTE
	
	#define LED2_PIN 2
	#define LED2_DDR DDRE
	#define LED2_PORT PORTE
	
	#define BUZZER_PIN 7
	#define BUZZER_DDR DDRB
	#define BUZZER_PORT PORTB
//finger LEDs
#warning "double check if the defines match the LED type on the hardware!!!"

	#define LEDRED_PIN 0
	#define LEDRED_DDR DDRG
	#define LEDRED_PORT PORTG
	
	#define LEDIR_PIN 1
	#define LEDIR_DDR DDRG
	#define LEDIR_PORT PORTG
//i2c device addresses
	#define DACFILTER_ADDR 0x98 | 2
	#define DACLED_ADDR    0x98 | 0
#warning "reminder: add addr for therm sensor"	
	#define THERM_ADDR	   0x00
//A/D channels, +1 to unconfuse schematic numbers to AD channel
	#define ADC_NOTCH 0+1 //LPF then notch output
	#define ADC_ALL   1+1 //LPF->NOTCH->HPF/AMP output
	#define ADC_LPF   2+1 //LPF output
	#define ADC_RAW   3+1 //raw sensor output
	#define ADC_BATT 0 //voltage divider of 6V battery to 3V
	#define ADC_PULLUPS 0x00 //bit values of 1 indicate pullups enabled
//RF transceiver pin mapping

	#define RFPORT PORTB
	#define RFDDR DDRB
	#define RFPIN PINB

	//#define RFGNDPIN 5

	#define TX_CE      0
	#define TX_CS      2
	#define RX_DR	   4
	#define TX_CLK1    3
	#define TX_DATA    5
	 
	#define RX_CE       TX_CE
	#define RX_CS       TX_CS
	#define RX_CLK1     TX_CLK1
	#define RX_DATA     TX_DATA

#else //lambda board defined
	#define LAMBDA_STR "\nLambda Board v0.7\n"
	#warning "***BUILDING LAMBDA BOARD"
	#warning "***BUILDING LAMBDA BOARD"
	#warning "***BUILDING LAMBDA BOARD"

//ports and pins
	#define LED_PIN 5
	#define LED_PORT PORTB
	#define LED_DDR DDRB

	#define BUZZER_PORT PORTA
	#define BUZZER_PIN 4
	#define BUZZER_DDR DDRA
	
	#define ADC_PULLUPS 0xFF //bit values of 1 indicate pullups enabled
//RF transceiver pin mapping
	#define RFPORT PORTC
	#define RFDDR DDRC
	#define RFPIN PINC

	#define RFGNDPIN 5

	#define TX_CE      0
	#define TX_CS      1
	#define RX_DR	   2
	#define TX_CLK1    3
	#define TX_DATA    4
	 
	#define RX_CE       TX_CE
	#define RX_CS       TX_CS
	#define RX_CLK1     TX_CLK1
	#define RX_DATA     TX_DATA
#endif

#endif
